package com.vdong.trade.trading.web.controller;

import com.alibaba.fastjson.JSON;
import com.vdong.trade.trading.common.contract.ApiContract;
import com.vdong.trade.trading.common.exception.ProcessException;
import com.vdong.trade.trading.common.foundation.FoundationController;
import com.vdong.trade.trading.entity.dto.param.ChannelEarningCurveParamDTO;
import com.vdong.trade.trading.entity.po.ChannelEarningCurvePO;
import com.vdong.trade.trading.entity.po.FinanceParamDTO;
import com.vdong.trade.trading.entity.vo.ChannelEarningCurveVO;
import com.vdong.trade.trading.entity.vo.EarningResultVO;
import com.vdong.trade.trading.service.FinanceService;
import lombok.extern.slf4j.Slf4j;
import org.lamb.framework.common.userDTO.AccountSystemUserInfoDTO;
import org.lamb.framework.common.util.CopyUtil;
import org.lamb.framework.common.util.DateUtil;
import org.lamb.framework.common.util.StringUtil;
import org.lamb.framework.core.security.LambPrincipalFactoryContainer;
import org.lamb.framework.core.templete.LambResponseTemplete;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

import static com.vdong.trade.trading.common.enums.ProcessExceptionEnum.EB00000008;

/**
 * @author zhoulongbiao
 * @Date 2018/12/12 13:38
 */
@RestController
@CrossOrigin
@Slf4j
public class FinanceController extends FoundationController {

    @Resource
    private FinanceService financeService;

    /**
     * 商户今日,本月,累计收入
     */
    @RequestMapping(value = ApiContract.I000000014, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> earnings(@RequestBody FinanceParamDTO param) {
        if (StringUtil.hasBlank(param.getUserId(), param.getUserType()))
            return Mono.error(new ProcessException(EB00000008));
        AccountSystemUserInfoDTO accountSystemUser = LambPrincipalFactoryContainer.getAccountSystemUser();
        if (!StringUtil.hasBlank(accountSystemUser.getSysUserId())) {
            param.setUserId(accountSystemUser.getSysUserId());
        }
        return returning(CopyUtil.copyObject(financeService.earnings(param), new EarningResultVO()));
    }

    /**
     * 渠道数据-收益
     */
    @RequestMapping(value = ApiContract.I000000015, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> channelEarningCurve(@RequestBody ChannelEarningCurvePO param) {
        if (StringUtil.hasBlank(param.getUserId())) return Mono.error(new ProcessException(EB00000008));
        log.info("channelEarningCurve,入参为:[{}]", JSON.toJSONString(param));
        AccountSystemUserInfoDTO accountSystemUser = LambPrincipalFactoryContainer.getAccountSystemUser();
        if (!StringUtil.hasBlank(accountSystemUser.getSysUserId())) {
            param.setUserId(accountSystemUser.getSysUserId());
        }
        if (!StringUtil.hasBlank(param.getStartDate(), param.getEndDate())) {
            return returning(CopyUtil.copyList(financeService.channelEarningCurve(ChannelEarningCurveParamDTO.builder()
                    .userId(param.getUserId()).startDate(DateUtil.stampToDate(param.getStartDate()))
                    .endDate(DateUtil.stampToDate(param.getEndDate())).build()), ChannelEarningCurveVO.class));
        }
        return returning(CopyUtil.copyList(financeService.channelEarningCurve(ChannelEarningCurveParamDTO.builder()
                .userId(param.getUserId()).build()), ChannelEarningCurveVO.class));
    }

}
